package com.cobra.mapreduce.reduceJoin;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

public class TableMapper extends Mapper<LongWritable , Text,Text,TableBean> {
    private String fileName;
    private Text outK = new Text();
    private TableBean outV = new TableBean();
    @Override
    //每个文件只需要执行一次setup方法
    protected void setup(Context context) throws IOException, InterruptedException {
        //初始化 order和pd表
        FileSplit fileSplit =(FileSplit) context.getInputSplit();
         fileName = fileSplit.getPath().getName();
    }
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
       //1 获取一行
        String line = value.toString();
        //2 判断文件来源
        String[] split = line.split("\\s+");
        if(fileName.contains("order")){
            //封装kv
            outK.set(split[1]);
            outV.setId(split[0]);
            outV.setPid(split[1]);
            outV.setAmount(Integer.parseInt(split[2]));
            outV.setPname("");
            outV.setFlag("order");
        }else {
            //封装kv
            outK.set(split[0]);
            outV.setId("");
            outV.setPid(split[0]);
            outV.setAmount(-1);
            outV.setPname(split[1]);
            outV.setFlag("pd");
        }
        context.write(outK,outV);
    }
}
